Main site

Sample codes > Using product tags

Using product tags

As with any LLM AI model outputs, always verify the generated code for accuracy before deploying it live.

For the sample prompts here, your might get different responses since AI models have random elements.

AI Prompt:

Generate a shipping code that uses the item product tags. 
If the product tags contain "free_shipping" offer a free shipping. 
If the product tags contain "standard_shipping", offer a $10 flat rate. 
Print the error message.

AI Output:

// Import required modules from "./modules.js"
import { enrichItemDetails,print } from "./modules.js";

export async function calculateShippingRates(DATA, env) {
    try {
        // Enrich item details to get product tags
        DATA = await enrichItemDetails(DATA);

        // Initialize rates array
        let rates = [];

        // Check each item in the DATA for product tags
        DATA.items.forEach(item => {
            if (item.product && item.product.tags) {
                // Check for "free_shipping" tag
                if (item.product.tags.includes("free_shipping")) {
                    rates.push({
                        service_name: "Free Shipping",
                        service_code: "FREE",
                        total_price: "0", // Free shipping
                        description: "Free Shipping",
                        currency: DATA.currency,
                        min_delivery_date: "", // Assuming no date info provided
                        max_delivery_date: ""
                    });
                }

                // Check for "standard_shipping" tag
                else if (item.product.tags.includes("standard_shipping")) {
                    rates.push({
                        service_name: "Standard Shipping",
                        service_code: "STD",
                        total_price: "1000", // $10 flat rate in cents
                        description: "Standard Shipping",
                        currency: DATA.currency,
                        min_delivery_date: "", // Assuming no date info provided
                        max_delivery_date: ""
                    });
                }
            }
        });
        
        return { rates };
    } catch (error) {
        // In case of errors, return rate with null price
        print("Error",error.message)
        return { 
            rates: [{
                service_name: "",
                service_code: "",
                total_price: null,
                description: "",
                currency: "",
                min_delivery_date: "",
                max_delivery_date: ""
            }]
        };
    }
}